clear all
set more off


set matsize 2000


include analysis\appendix\discretiz.ado
cls



/////////////////////////////////////////////////////////////////////
///FIGURE A.5///////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
use "finaldata\final_cc_data.dta", clear

drop if fipsplace_00==.
xtset fipsplace_00 year

drop if year>1991

*Create outcome
g perc_cc =  tot_cc_oripop_corr / (tot_ncc_oripop_corr + tot_cc_oripop_corr)

*Create dependent variables
gen vc_pc=totnpcc_cc_offenses_vc*1000

gen ph10=ph1_plc_wtm_wtm_0_r*10 // Stata loops do not work well with decimals

discretiz ph10, range(60/80) interval(5(1)10) endogenous(vc_pc) exogenous(i.year i.year#i.cdivcode) interact(tetra_corr) xt(fe) graph(coef)

discretiz ph10, range(60/80) interval(5(1)10) endogenous(vc_pc) exogenous(i.year i.year#i.cdivcode) interact(tetra_corr) xt(fe) graph(fstat)

/////////////////////////////////////////////////////////////////////
///FIGURE A.6///////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////

discretiz ph10, range(60/80) interval(5(1)10) endogenous(vc_pc) exogenous(i.year i.year#i.cdivcode) interact(tetra_corr) xt(fe) print



/////////////////////////////////////////////////////////////////////
///FIGURE A.7///////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////

use "finaldata\final_cc_data.dta", clear
drop if fipsplace_00==.
xtset fipsplace_00 year

drop if year>1991

*Create instrument and outcome
g good_soil = (ph1_plc_wtm_wtm_0_r>=6.8 & ph1_plc_wtm_wtm_0_r<=7.7)
gen inter_goodtetra=good_soil*tetra_corr
g perc_cc =  tot_cc_oripop_corr / (tot_ncc_oripop_corr + tot_cc_oripop_corr)

*Create standardized variables
egen sd_vc=sd(totnpcc_cc_offenses_vc)
egen sd_pop_cc=sd(tot_cc_oripop_corr)
egen sd_pop_ncc=sd(tot_ncc_oripop_corr)
gen vc_pc=totnpcc_cc_offenses_vc * 1000
gen standardized_pop_cc=tot_cc_oripop_corr/sd_pop_cc
gen standardized_pop_ncc=tot_ncc_oripop_corr/sd_pop_ncc


gen tetra_corr_19=tetra_corr
forvalues i=1/10{
local j=19+`i'
local l=19-`i'
gen tetra_corr_`j'=L`i'.tetra_corr
gen tetra_corr_`l'=F`i'.tetra_corr
}

drop if year>1991

gen inter_goodtetra_9=good_soil*tetra_corr_9
reghdfe vc_pc inter_goodtetra_9, absorb(fipsplace_00 year cdivcode#year) vce(robust)
test _b[inter_goodtetra_9]=0
local ff = trim("`: display %10.2f r(F)'")
gen beta_9=_b[inter_goodtetra_9]
gen cilow_9=_b[inter_goodtetra_9]-1.96*_se[inter_goodtetra_9]
gen cihigh_9=_b[inter_goodtetra_9]+1.96*_se[inter_goodtetra_9]
gen fstat_9=`r(F)'

forvalues i=10/29{
gen inter_goodtetra_`i'=good_soil*tetra_corr_`i'
reghdfe vc_pc inter_goodtetra_`i', absorb(fipsplace_00 year cdivcode#year) vce(robust)
test _b[inter_goodtetra_`i']=0
local ff = trim("`: display %10.2f r(F)'")
gen beta_`i'=_b[inter_goodtetra_`i']
gen cilow_`i'=_b[inter_goodtetra_`i']-1.96*_se[inter_goodtetra_`i']
gen cihigh_`i'=_b[inter_goodtetra_`i']+1.96*_se[inter_goodtetra_`i']
gen fstat_`i'=`r(F)'
}


keep beta_* ci* fstat* 
drop cincome

gen n=_n
keep if n==1
gen beta=.
gen cilow=.
gen cihigh=.
gen fstat=.
gen lag=.
forvalues i=9(1)29{
expand 2 if _n==1, gen(dup)
replace lag=`i' if dup==1
replace beta=beta_`i' if dup==1
replace cilow=cilow_`i' if dup==1
replace cihigh=cihigh_`i' if dup==1
replace fstat=fstat_`i' if dup==1
drop dup
}

drop if lag==.

keep lag beta cilow cihigh fstat


label var beta "Coefficient"
label var fstat "F-statistics"
label var cilow "Lower CI"
label var cihigh "Upper CI"

twoway   (line fstat lag, yaxis(1) xlabel(9(3)29) ytitle("F-Stat") xtitle("Lag of national lead")) 
